
#include<reg51.h>
#include<absacc.h>
sbit bp20=P2^0;//????
sbit bp21=P2^1;
sbit bp22=P2^2;
sbit bp23=P2^3;
sbit bp24=P2^4;
sbit bp25=P2^5;
void InitCom(unsigned char BaudRate)
{
unsigned char THTL;
switch (BaudRate)
{
case 1: THTL = 64; break; //???300
case 2: THTL = 160; break; //600
case 3: THTL = 208; break; //1200
case 4: THTL = 232; break; //2400
case 5: THTL = 244; break; //4800
case 6: THTL = 250; break; //9600
case 7: THTL = 253; break; //19200
case 8: THTL = 255; break; //57600
default: THTL = 208;
}
SCON = 0x50; //????1,????
TMOD = 0x20; //???1????2
TCON = 0x40; //????1????
TH1 = THTL;
TL1 = THTL;
PCON = 0x80; //???????,SMOD?
RI = 0; //?????
TI = 0;
TR1 = 1; //?????
}


//?????????(?????)
unsigned char ComInChar()
{
unsigned char InData;
while(!RI); //???????????
InData = SBUF; //??COM????
RI = 0; //?RI
return (InData); //???????
}

void hmcode_Decoder(unsigned char xx,unsigned char yy)//????????
{unsigned char  zxh,nu0,nu1,nu2,nu3,nu4,nu5,nu6,nu7,y,error1,error2,error3,error4,code1,data_in_H,data_in_L,error;
bit a,b,c,d;
bit data_in_L4,data_in_L5,data_in_L6,data_in_L7;
bit data_in_H1,data_in_H2,data_in_H3,data_in_H4,data_in_H5,data_in_H6,data_in_H7,data_in_H0;

 
 data_in_H=xx;
y=data_in_H;
if((y & 0x80)==0x80) data_in_H7=1;else data_in_H7=0;
if((y & 0x40)==0x40) data_in_H6=1;else data_in_H6=0;
if((y & 0x20)==0x20) data_in_H5=1;else data_in_H5=0;
if((y & 0x10)==0x10) data_in_H4=1;else data_in_H4=0;
if((y & 0x08)==0x08) data_in_H3=1;else data_in_H3=0;
if((y & 0x04)==0x04) data_in_H2=1;else data_in_H2=0;
if((y & 0x02)==0x02) data_in_H1=1;else data_in_H1=0;
if((y & 0x01)==0x01) data_in_H0=1;else data_in_H0=0;
data_in_L=yy;
y=data_in_L;
if((y & 0x80)==0x80) data_in_L7=1;else data_in_L7=0;
if((y & 0x40)==0x40) data_in_L6=1;else data_in_L6=0;
if((y & 0x20)==0x20) data_in_L5=1;else data_in_L5=0;
if((y & 0x10)==0x10) data_in_L4=1;else data_in_L4=0; 

a=data_in_L4^data_in_L6^data_in_H0^data_in_H2^data_in_H6^data_in_H4; //??A?
b=data_in_L5^data_in_L6^data_in_H1^data_in_H2^data_in_H6^data_in_H5; //??B?
c=data_in_L7^data_in_H0^data_in_H1^data_in_H2^data_in_H7; //??C?
d=data_in_H3^data_in_H4^data_in_H5^data_in_H6^data_in_H7; //??D?
error=0x00;

if(a==1) error1=1; else error1=0;
if(b==1) error2=2; else error2=0;
if(c==1) error3=4; else error3=0;
if(d==1) error4=8; else error4=0;
error=error1+error2+error3+error4;

if(error==3) data_in_L6=~data_in_L6; //???????1,????????
if(error==5) data_in_H0=~data_in_H0; //???????1,????????
if(error==6) data_in_H1=~data_in_H1; //???????1,????????
if(error==7) data_in_H2=~data_in_H2; //???????1,????????
if(error==9) data_in_H4=~data_in_H4; //???????1,????????
if(error==10) data_in_H5=~data_in_H5; //???????1,????????
if(error==11) data_in_H6=~data_in_H6; //???????1,????????
if(error==12) data_in_H7=~data_in_H7;
 
//
if(data_in_L6==0) nu0=0x00; else  nu0=0x01;  
if(data_in_H0==0) nu1=0x00; else  nu1=0x02;
if(data_in_H1==0) nu2=0x00; else  nu2=0x04;
if(data_in_H2==0) nu3=0x00; else  nu3=0x08;
if(data_in_H4==0) nu4=0x00; else  nu4=0x10;
if(data_in_H5==0) nu5=0x00; else  nu5=0x20;
if(data_in_H6==0) nu6=0x00; else  nu6=0x40;
if(data_in_H7==0) nu7=0x00; else  nu7=0x80;
  code1=nu0 | nu1 | nu2 | nu3 | nu4 | nu5 | nu6 | nu7;
DBYTE[0x40]=code1;

zxh=nu4 | nu5 | nu6 | nu7;
zxh=zxh>>4;
DBYTE[0X41]=zxh;
DBYTE[0X42]=nu0 | nu1 | nu2 | nu3;
}

void delay(unsigned char t)
{unsigned char i,j;
for(i=0;i<t;i++)
   for(j=0;j<=200;j++)
  ;
}



main()
  {unsigned char x,y,aah,aal,k,i,a[5];
unsigned char code seg7[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};
InitCom(4);//?2400BPS
i=0;
aah=0;
aal=0;
while(1)
{a[i]=ComInChar();
i=i+1;
if(i==3) {i=0;k=1;} else k=0;
if(k==1) {x=a[0]<<4;
x=x | a[1];
y=a[2]<<4;
y=y | 0x00;
hmcode_Decoder(x,y);
aah=DBYTE[0X41];
aal=DBYTE[0X42];}
P0=seg7[a[0]];
bp20=1;
delay(10);
bp20=0;
bp23=0;
P0=seg7[a[1]];
bp21=1;
delay(10);
bp21=0;
P0=seg7[a[2]];
bp22=1;
delay(10);
bp22=0;
P0=seg7[aah];
bp24=1;
delay(10);
bp24=0;
P0=seg7[aal];
bp25=1;
delay(10);
bp25=0;
}
}   